<?php
/*interface KeywordProvider
{
	public function setKeywords($value);
	public function getKeywords($value);
}

class DBKeywordProvider implements KeywordProvider
{
	private $link;
    private $query;
    private $result;
    private $keyword;
	private $keywords = array();
	
	private static function ConnectDb()
	{
		if (mysql_connect(HOST, USER, USERPASSWORD ) && mysql_select_db( DBNAME ))
		return true; 
		else return false;
	}
	
	public function SetKeywords($value)
	{
	if (ConnectDB)
		mysql_query("INSERT INTO keywords.id, keywords.value, keywords.active VALUES('','$kw','$status')");
	}
    public function GetKeywords()
    {
    if (ConnectDB)
    	mysql_query("SELECT keywords.value from keywords WHERE keywords.active=1");
    	
    
	}
}
class UserKeywordProvider implements KeywordProvider
{
	private $keywords = array();
	public static function SetKeywords($value)
	{
		$this->keyword = $value;
	}
    public function GetKeywords()
    {
	return $this->keyword;	
	}
}
class KeywordProviderFactory
{
	public static function CreateProvider($type) {
		if($type == 'userinput')
		{
			return new UserKeywordProvider();
		} else 
		return new DBKeywordProvider();
		
	}
}*/
/*$search = KeywordProviderFactory::CreateProvider(SOURCE);
$keywords = $search->GetKeywords();*/
include_once('config.php');
include_once('simple_html_dom.php');

class Parser
{
	private $keyword;
	private $html;
	private $keystring;
	private $counter = 0;
	private $item;
	private $article;
	private $pages;
	private $position = false;
	
	function __construct ()
	{
		if ($_POST['userkeyword']<>'')
		{
		$this->SetKeyword($_POST['userkeyword']);
		$this->Setpages = (DEPTH/10);
		ini_set('user_agent', 'My-Application/2.5');
		$this->Parsing_google($this->GetKeyword());
		}
		else echo 'No keyword-no parsing!';
		sleep(3);
		#header('Location:index.php');
	}
	
	private function SetKeyword($value)
	{
		$this->keyword = $value;
	}
    private function GetKeyword()
	{
		return $this->keyword;
	}
	
   /* private function Setpages($value)
	{
		$this->pages = $value;
	}
    private function Getpages()
	{
		return $this->pages;
	}*/
	private function Scrap()
	{   
		for($i = 0; $i <DEPTH; $i+=10)
		{
		$this->html = file_get_html(ENGINE . '/search?hl=uk&q=' . $this->keystring . '&start=' . $i . '&sa=N');
		foreach($this->html->find('#res li') as $this->article) 
		{
			$this->item['href'] = trim($this->article->find('h3 a', 0)->href);
			if (strpos($this->item['href'], URL) <>0)
			{
				$this->counter++;
				$this->position = true;
				break;
			}
			$this->counter++;
		}
		if ($this->position)
		 break;
		}
		return $this->counter;
	}
	
	private function Parsing_google($value) {
		$this->keystring = str_replace(' ', '+', trim($value));
		self::Scrap();
	    $this->html->clear();
        unset($this->html);
        echo $this->counter;
}
}
?>